<!--
 * @Author: 袁十一
 * @Date: 2023-09-25 19:20:24
 * @LastEditTime: 2023-10-12 00:28:12
 * @LastEditors: Please set LastEditors
 * @Description: 
 * @FilePath: \Vue3_admin_template\src\views\login\index.vue
 * 初心不变，意志坚定
-->
<template>
  <div class="container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form class="login_form">
          <h1>Hello</h1>
          <h2>欢迎来到硅谷甄选</h2>
          <el-form :rules="rules" ref="Loginforms" :model="LoginForm">
            <el-form-item prop="username">
              <el-input
                :prefix-icon="User"
                v-model="LoginForm.username"
              ></el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                type="password"
                show-password
                :prefix-icon="Lock"
                v-model="LoginForm.password"
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-button
                :loading="loding"
                class="btn"
                type="primary"
                size="default"
                @click="login"
              >
                登录
              </el-button>
            </el-form-item>
          </el-form>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { User, Lock } from '@element-plus/icons-vue'
import { ref } from 'vue'
import { reactive } from 'vue'
import useUserStore from '@/store/modules/user'
import { useRouter, useRoute } from 'vue-router'
import { ElNotification } from 'element-plus'
import { getTime } from '@/util/time'
let userStore = useUserStore()
let LoginForm = reactive({ username: 'admin', password: 'atguigu123' })
let $router = useRouter()
let loding = ref(false)
let Loginforms = ref()
let $route = useRoute()
const login = async () => {
  // let result = userStore.userLogin(LoginForm);
  //   console.log(result);
  //表单校验回调
  await Loginforms.value.validate()
  //登录按钮动销配置
  loding.value = true

  try {
    await userStore.userLogin(LoginForm)
    //
    let redirect: any = $route.query.redirect
    $router.push({ path: redirect || '/home' })
    // 登录成功的提示
    ElNotification({
      type: 'success',
      message: '欢迎回来',
      title: `HI,${getTime()}好!`,
    })
    loding.value = false
  } catch (error) {
    ElNotification({
      type: 'error',
      message: (error as Error).message,
    })
    loding.value = false
  }
}
const validateUsername = (_rule: any, value: any, callback: any) => {
  // rule:即为校验规则对象
  // value:即为表单元素内容
  // 函数：如果符合条件callback放行通过即可
  // 如果不符合条件callback方法，注入错误提示信息
  if (value.length >= 5 && value.length <= 10) {
    callback()
  } else {
    callback(new Error('账号至少5位'))
  }
}
const validatePassword = (_rule: any, value: any, callback: any) => {
  // rule:即为校验规则对象
  // value:即为表单元素内容
  // 函数：如果符合条件callback放行通过即可
  // 如果不符合条件callback方法，注入错误提示信息
  const rulePassword = /^(?=.*\d)(?=.*[a-zA-Z]).{6,10}$/
  if (rulePassword.test(value)) {
    callback()
  } else {
    callback(new Error('至少包含字母、数字、特殊字符,6-10位'))
  }
}
const rules = reactive({
  username: [{ validator: validateUsername, trigger: 'blur' }],
  password: [
    { validator: validatePassword, min: 6, max: 16, trigger: 'change' },
  ],
})
</script>

<style scoped lang="scss">
.container {
  width: 100%;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  overflow: hidden;

  .login_form {
    width: 80%;
    position: relative;
    top: 30vh;
    padding: 40px;
    box-sizing: border-box;
    background: url('@/assets/images/login_form.png') no-repeat;
    background-size: cover;

    h1 {
      color: white;
      font-size: 40px;
    }

    h2 {
      color: white;
      font-size: 20px;
      margin: 20px 0;
    }

    .btn {
      width: 100%;
    }
  }
}
</style>
